package neuronet.server;

import java.util.Iterator;
import java.util.Vector;

import neuronet.common.NeuroNetCom;

import org.apache.log4j.Logger;

/**
 * This class hold the corresponding date of each client stored in the table
 */
public class ClientsTableItem {
    // Logger
    private static Logger logger = Logger.getLogger(ClientsTableItem.class);

    private String clientID;
    
    private String clientKey;
    private String clientInfo;
    private Vector<String> dataItems;
    private Vector<String> targets;
    private Vector<String> messages;
//    private TCPThread tcpThread;
    private long lastChecked = 0;

    public ClientsTableItem(String clientID, String clientInfo)
    {
	this.clientInfo = clientInfo;
	this.clientID = clientID;
	this.clientKey = clientID + "" + System.currentTimeMillis();
	// TODO: uncommet next line
	//	this.clientKey = clientID;
	this.dataItems = new Vector<String>();
	this.setTargets(new Vector<String>());
//	this.tcpThread = tcpThread;
	messages = new Vector<String>();
    }


    public synchronized String setMessage(String senderID, String data)
    {
	String storeMessage = "incomingtime:" + System.currentTimeMillis() + " senderid:" + senderID + " " + "data[" + data + "]";
	messages.add(storeMessage);

	return "status:ok";
	//	return tcpThread.sendToClient(senderID, data);
    }

    public synchronized Vector<String> getMessages()
    {
	Vector<String> someMessages = new Vector<String>();
	for (Iterator<String> messIt = messages.iterator(); messIt.hasNext();)
	{
	    String mess = messIt.next();
	    
	    long inTime = Long.valueOf(NeuroNetCom.getParameterFormMessage("incomingtime", mess));
	    if(inTime > lastChecked)
	    {
		logger.info("---------- inTime > lastChecked -> " + inTime+" > "+lastChecked);
		someMessages.add(mess);
		
	    }
	    
	}
	
	logger.info("---------- lastChecked = " +lastChecked);
	lastChecked = System.currentTimeMillis();
	
	return someMessages;
    }
    // Getters and Setters
    
    public Vector<String> getDataItems()
    {
	return dataItems;
    }
    
    public synchronized  void setDataItems(Vector<String> dataItems)
    {
	this.dataItems = dataItems;
    }
    
    public String getClientKey()
    {
	return clientKey;
    }
    
    public String getClientInfo()
    {
	return clientInfo;
    }
    
    public void setClientKey(String clientKey)
    {
	this.clientKey = clientKey;
    }
    
    public void setClientInfo(String clientInfo)
    {
	this.clientInfo = clientInfo;
    }
    
    public synchronized  void setDataTargets(Vector<String> dataTargets)
    {
	this.setTargets(dataTargets);
    }
    
    public Vector<String> getDataTargets()
    {
	return getTargets();
    }

    public Vector<String> getAllMessages()
    {
	return messages;
    }
    public String getClientLast()
    {
	return "" + lastChecked;
    }
    public String getClientIP()
    {
	return "not_implemented";
    }
    public String getClientPort()
    {
	return "not_implemented";
    }
    public String getClientID()
    {
	return clientID;
    }
    public void setClientID(String clientID)
    {
	this.clientID = clientID;
    }

    public void setTargets(Vector<String> targets)
    {
	this.targets = targets;
    }

    public Vector<String> getTargets()
    {
	return targets;
    }

}
